#include "output.h"
#include "src/vector3d.h"
#include "src/object.h"

#include <QDebug>
#include <QString>
#include <QFileInfo>
#include <iostream>

Output::Output(){
    file = NULL;
}

Output::~Output(){
}

void Output::parseObjects(QFile *_file, Scene *_s){
	Object *object;
    file = _file;

    file->open(QFile::WriteOnly);

	for(unsigned int i = 0; i < _s->getObjectsSize(); i++){
	    object = _s->getObject(i);
	    //cada linha representará um objeto
	    writeObject(object);
	}

    file->close();
}

void Output::parseObjects(Scene *_s){
	Object *object;

    file->open(QFile::WriteOnly);
	for(unsigned int i = 0; i < _s->getObjectsSize(); i++){
	    object = _s->getObject(i);
	    //cada linha representará um objeto
	    writeObject(object);
	}
    file->close();
}

void Output::writeObject(Object *_obj, int _parentId){
	QTextStream stream(file);
//
//    int type = _obj->getType();
//    int id = _obj->getId();
//    Material m = _obj->getMaterial();
//    const double *ambient = m.getAmbient();
//    const double *diffuse = m.getDiffuse();
//    const double *specular = m.getSpecular();
//    Vector3D center = _obj->getCenter();
//    Vector3D relativePos;// = _obj->getRelativePosition();
//    Transformation trans = _obj->getTransformation();
//    Matrix matrix = trans.getLocalToWorld();
//    Matrix matrixI = trans.getWorldToLocal();
//
//    if(_obj->isPrimitive()){
//        stream << "p " << id << " " << type  << " " << _parentId //p de primitiva
//                  << " " << ambient[0] << " " << ambient[1] << " " << ambient[2]
//                  << " " << diffuse[0] << " " << diffuse[1] << " " << diffuse[2]
//                  << " " << specular[0] << " " << specular[1] << " " << specular[2]
//                  << " " << center.x << " " << center.y << " " << center.z
//                  << " " << relativePos.x << " " << relativePos.y << " " << relativePos.z
//                  << " " << matrix[0][0] << " " << matrix[0][1] << " " << matrix[0][2] << " " << matrix[0][3]
//                  << " " << matrix[1][0] << " " << matrix[1][1] << " " << matrix[1][2] << " " << matrix[1][3]
//                  << " " << matrix[2][0] << " " << matrix[2][1] << " " << matrix[2][2] << " " << matrix[2][3]
//                  << " " << matrix[3][0] << " " << matrix[3][1] << " " << matrix[3][2] << " " << matrix[3][3]
//                  << " " << matrixI[0][0] << " " << matrixI[0][1] << " " << matrixI[0][2] << " " << matrixI[0][3]
//                  << " " << matrixI[1][0] << " " << matrixI[1][1] << " " << matrixI[1][2] << " " << matrixI[1][3]
//                  << " " << matrixI[2][0] << " " << matrixI[2][1] << " " << matrixI[2][2] << " " << matrixI[2][3]
//                  << " " << matrixI[3][0] << " " << matrixI[3][1] << " " << matrixI[3][2] << " " << matrixI[3][3]
//                  << " ";
//        switch(type){
//            case CUBOID:{
//                Vector3D min = ((CuboidPrimitive *)_obj)->getMin();
//                Vector3D max = ((CuboidPrimitive *)_obj)->getMax();
//                stream << min.x << " " << min.y << " " << min.z << " " << max.x << " " << max.y << " " << max.z << endl;
//                break;
//            }
//            case SPHERE:{
//                double radius = ((SpherePrimitive *)_obj)->getRadius();
//                stream << radius << endl;
//                break;
//            }
//            case CYLINDER:{
//                double radius = ((CylinderPrimitive *)_obj)->getRadius();
//                double height = ((CylinderPrimitive *)_obj)->getHeight();
//                stream << radius << " " << height << endl;
//                break;
//            }
//            case CONE:{
//                double radius = ((ConePrimitive *)_obj)->getRadius();
//                double height = ((ConePrimitive *)_obj)->getHeight();
//                stream << radius << " " << height << endl;
//                break;
//            }
//        }
//
//    }else{
//        stream << "o " << id << " " << type  << " " << _parentId //p de primitiva
//                  << " " << ambient[0] << " " << ambient[1] << " " << ambient[2]
//                  << " " << diffuse[0] << " " << diffuse[1] << " " << diffuse[2]
//                  << " " << specular[0] << " " << specular[1] << " " << specular[2]
//                  << " " << center.x << " " << center.y << " " << center.z
//                  << " " << relativePos.x << " " << relativePos.y << " " << relativePos.z
//                  << " " << matrix[0][0] << " " << matrix[0][1] << " " << matrix[0][2] << " " << matrix[0][3]
//                  << " " << matrix[1][0] << " " << matrix[1][1] << " " << matrix[1][2] << " " << matrix[1][3]
//                  << " " << matrix[2][0] << " " << matrix[2][1] << " " << matrix[2][2] << " " << matrix[2][3]
//                  << " " << matrix[3][0] << " " << matrix[3][1] << " " << matrix[3][2] << " " << matrix[3][3]
//                  << " " << matrixI[0][0] << " " << matrixI[0][1] << " " << matrixI[0][2] << " " << matrixI[0][3]
//                  << " " << matrixI[1][0] << " " << matrixI[1][1] << " " << matrixI[1][2] << " " << matrixI[1][3]
//                  << " " << matrixI[2][0] << " " << matrixI[2][1] << " " << matrixI[2][2] << " " << matrixI[2][3]
//                  << " " << matrixI[3][0] << " " << matrixI[3][1] << " " << matrixI[3][2] << " " << matrixI[3][3]
//                  << endl;
//        for(unsigned int i = 0; i < ((CombinedObject *)_obj)->getObjectsSize(); i++){
//            Object *obj = ((CombinedObject *)_obj)->getObject(i);
//            writeObject(obj, id);
//        }
//    }
}

bool Output::isFileNull(){
    if(file) return false;
    return true;
}

void Output::setFile(QFile *_file){
    file = _file;
}
